<template>
	<view class="bindPhone">
		<view class="weChatLogin">
			<view class="avatar">
				<image class="img" src="@/static/loginImg/toux.jpg"></image>
			</view>
			<text style="font-weight: 700; margin-top: 20rpx; font-size: 30rpx">住享家</text>
			<view class="weChatBtn">
				<button class="btn" @click="wechatLogin">微信用户快速登录</button>
			</view>
		</view>
		<view class="zbdl" @click="resetBtn()">暂不登录</view>
	</view>
</template>

<script>
	import {
		wxLoginAuthorization,
		getUserInfo
	} from '@/api/user'
	const BACK_URL = "login_back_url";
	export default {
		name: "Login",
		data() {
			return {

			}
		},
		methods: {
			resetBtn() {
				uni.navigateTo({
					url: '/pages/users/login/weChatLogin',
				});
			},
			wechatLogin() {
				let that = this
				uni.showLoading({
					title: '登录中'
				})
				wx.login({
					provider: 'weixin', //使用微信登录
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success(res) {
						if (res.code) {
							// 获取用户信息
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									console.log('获取用户信息', infoRes.userInfo)
									let params = {
										"avatar": infoRes.userInfo.avatarUrl,
										"code": res.code,
										"nickName": infoRes.userInfo.nickName,
										"spread_spid": 0
									}
									wxLoginAuthorization(params).then(async ({
										data
									}) => {
										that.$store.commit("LOGIN", {
											'token': data.token
										});
										await that.getUserInfo(data);
									}).catch(e => {
										that.$util.Tips({
											title: e
										});
									});
								},
							})
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			async getUserInfo(data) {
				this.$store.commit("SETUID", data.uid);
				try {
					const res = await getUserInfo();
					this.$store.commit("UPDATE_USERINFO", res.data);
					let backUrl = this.$Cache.get(BACK_URL) || "/pages/index/index";
					if (backUrl.indexOf('/pages/users/login/index') !== -1) {
						backUrl = '/pages/index/index';
					}
					if (!backUrl.startsWith('/')) {
						backUrl = `/${backUrl}`;
					}
					uni.switchTab({
						url: backUrl,
						fail: () => {
							console.log('fail==++++++');
							uni.reLaunch({
								url: backUrl,
								fail: () => {
									console.log('fail');
								}
							});
						}
					});
				} catch (error) {
					console.error("获取用户信息失败", error);
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.bindPhone {
		width: 100%;
		height: 100vh;
		padding: 40rpx;

		.weChatLogin {
			margin: 0 auto;
			display: flex;
			flex-direction: column;
			justify-content: center;
			margin-top: 120rpx;
			align-items: center;

			.avatar {
				width: 300rpx;
				height: 300rpx;
				border-radius: 50%;
				overflow: hidden;

				.img {
					width: 100%;
					height: 100%;
					border-radius: 50%;
					display: block;
				}
			}

			.weChatBtn {
				margin-top: 270rpx;

				.btn {
					width: 500rpx;
					height: 80rpx;
					line-height: 80rpx;
					margin-bottom: 50rpx;
					background-color: #13c225;
					color: #f5f5f5;
					border-radius: 30rpx;
					margin-bottom: 100rpx;
				}
			}

		}

		.zbdl {
			width: 100%;
			text-align: center;
		}
	}
</style>